<?php
/* 
 * ebiz front
 * 
 * Config Model
 * 
 * @author allen
 * 
 */
require_once '../app/core/models/BaseDao.php';
require_once '../app/core/db/DBUtil.class.php';

class ShoppingCartDao extends BaseDao 
{
	
    private $tableName = "shopping_carts";   
     
    public function __construct($dbUtil){ 
        parent::__construct($dbUtil);
    }
    
    public function getShoppingCartById($id){
    	$param = array($id);
    	$sql = "select * from $this->tableName where id = ? and is_delete = 0";
        return $this->dbUtil->SelectData($sql, $param);
    }
    
    public function getShoppingCartByCustomerId($cid){  
    	$param = array($cid);
    	$sql = "select * from $this->tableName where customer_id = ? and is_delete = 0";
        return $this->dbUtil->SelectData($sql, $param);
    }
    
    public function deleteShoppingCartById($scId){    //delete user 
        $param = array($scId);
        $sql = "update $this->tableName set is_delete = 1 where id = ? ";
        return $this->dbUtil->ExecuteQuery($sql,$param);
    }
    
    public function deleteShoppingCartByCustomerId($cid){    //delete user 
        $param = array($cid);
        $sql = "update $this->tableName set is_delete = 1 where customer_id = ? ";
        return $this->dbUtil->ExecuteQuery($sql, $param);
    }
    
//    public function deleteShoppingCartByCIdAndItemId($cid, $itemId){    //delete user//not use 
//        $param = array($cid, $itemId);
//        $sql = "update $this->tableName set is_delete = 1 where customer_id = ? and product_item_id = ?";
//        return $this->dbUtil->ExecuteQuery($sql, $param);
//    }
    
    public function updateQuantityByItemId($customerId, $pItemId, $quantity){   //update quantity in shopping cart view 
        $param = array($quantity, $pItemId, $customerId);
        $sql = "update $this->tableName set quantity = ? where product_item_id = ? and customer_id = ? and is_delete = 0";
        return $this->dbUtil->ExecuteQuery($sql, $param);
    }
        
    public function getPriceAndQuantityByCustomerId($cid){  //show info in header view
    	$param = array($cid);
    	$sql = "select  sc.quantity , pi.sale_price , pi.product_id as pid  
    	from shopping_carts as sc 
    	join product_items as pi  on  sc.product_item_id = pi.id and pi.is_delete = 0 
    	where sc.customer_id = ? and sc.is_delete = 0 ";   
        return $this->dbUtil->SelectData($sql, $param);
    }
    
    public function saveShoppingCart($param){
        $sql = "insert into $this->tableName (product_id, product_item_id, customer_id, quantity, sale_price, modified, created) values (?,?,?,?,?,now(),now())";
        return $this->dbUtil->ExecuteQuery($sql, $param);
    	
    }
    
    public function getShoppingCartByItemIdAndCid($itemId, $cid){
        $param = array($itemId, $cid);
        $sql = "select * from $this->tableName where product_item_id = ? and customer_id = ? and is_delete = 0";
        return $this->dbUtil->SelectData($sql, $param);
    }
    
    public function addQuantityByItemId($customerId, $pItemId, $quantity){   //update quantity in shopping cart view 
        $param = array($quantity, $pItemId, $customerId);
        $sql = "update $this->tableName set quantity = quantity + ? where product_item_id = ? and customer_id = ? and is_delete = 0";
        return $this->dbUtil->ExecuteQuery($sql, $param);
    }
    
    public function updateStateById($customerId, $pItemId, $quantity){ //update shoppingCart info include state through itemId and customerId,if state=1 then this is checkout item     
        $param = array($quantity, $pItemId, $customerId);
        $sql = "update $this->tableName set quantity = ?,status = 1 where product_item_id = ? and customer_id = ? and is_delete = 0";
        return $this->dbUtil->ExecuteQuery($sql, $param);
    }
    
    public function updateStateForClearById($customerId){ //update state=0 before to checkout view     
        $param = array($customerId);
        $sql = "update $this->tableName set status = 0 where customer_id = ? and is_delete = 0";
        return $this->dbUtil->ExecuteQuery($sql, $param);
    }
    
    public function getShoppingcartInfoToCheckoutByCustomerId($customerId){ //get the item info for checkout page
        $param = array($customerId);
        //not use product image table //modify at 0605
        $sql = "select sc.id as scId , sc.quantity  ,  pi.id as pItemId ,  
        pi.attribute_1 , pi.value_1 , pi.attribute_2 , pi.value_2 ,  pi.attribute_3 , pi.value_3 ,  pi.attribute_4 , pi.value_4,  pi.attribute_5, pi.value_5 , 
        pi.sale_price , pi.upc  ,  pi.weight ,
        p.title ,   p.cheque_rule , p.ship_rule 
        from  product_items pi join products p on p.id = pi.product_id  
        join $this->tableName sc on sc.product_item_id = pi.id  and sc.is_delete = 0  and customer_id = ? and sc.status = 1 
        where pi.is_delete = 0 and p.is_delete = 0 ";
//        $sql = "select sc.id as scId , pi.id as pItemId ,  pi.attribute_1 , pi.value_1 , pi.attribute_2 , pi.value_2 ,  pi.attribute_3 , pi.value_3 ,  pi.attribute_4 , pi.value_4,  pi.attribute_5, pi.value_5 , pi.sale_price ,  p.title ,  pim.name , pim.folder , sc.quantity  from  $this->tableName pi join products p on p.id = pi.product_id  join shopping_carts sc on sc.product_item_id = pi.id  and sc.is_delete = 0  and customer_id = ? and sc.status = 1 left join product_images pim on pim.item_id = pi.id and pim.default_item_image = 1 and pim.is_delete = 0 where pi.is_delete = 0 and p.is_delete = 0 ";
//        $sql = "select sc.id as scId , pi.id as pItemId ,  pi.attribute_1 , pi.value_1 , pi.attribute_2 , pi.value_2 ,  pi.attribute_3 , pi.value_3 ,  pi.attribute_4 , pi.value_4,  pi.attribute_5, pi.value_5 , pi.sale_price ,  p.title ,  pim.name , pim.folder , sc.quantity  from  $this->tableName pi join products p on p.id = pi.product_id  join shopping_carts sc on sc.product_item_id = pi.id  and sc.is_delete = 0  and customer_id = ?                   left join product_images pim on pim.item_id = pi.id and pim.default_item_image = 1 and pim.is_delete = 0 where pi.is_delete = 0 and p.is_delete = 0  and sc.status = 1 ";
        return $this->dbUtil->SelectData($sql, $param);
    }

    /**
     * get the qty and inventory info from shoppingcart,product,product_item to get the info to show prompt info
     */
    public function getInventoryInfoByItemIdAndCid($itemIds, $customerId){
    	$itemIds = mysql_escape_string($itemIds);
    	$param = array($customerId);
    	$sql = "select pi.id as pItemId , pi.inventory ,  
		pi.attribute_1 , pi.value_1 , pi.attribute_2 , pi.value_2 ,  pi.attribute_3 , pi.value_3 ,  pi.attribute_4 , pi.value_4,  pi.attribute_5, pi.value_5 , pi.sale_price ,
		pi.is_delete as piIsDelete , 
		p.title , p.is_delete as pIsDelete,
		sc.id as scId , sc.quantity
		from product_items pi
		join products p on p.id = pi.product_id  
		join shopping_carts sc on sc.product_item_id = pi.id and sc.is_delete = 0 and sc.status = 1 and sc.customer_id = ? 
		where pi.id  in  ( $itemIds ) ";
    	return $this->dbUtil->SelectData($sql, $param);
    }

    
//    public function updateQuantityById($scId, $quantity){   //update quantity in shopping cart view 
//        $param = array($quantity, $scId);
//        $sql = "update $this->tableName set quantity = ? where id = ? ";
//        return $this->dbUtil->ExecuteQuery($sql, $param);
//    }
    
}



